﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;

namespace UserGroupData
{
    public class MemberPaymentRepository : IMemberPaymentRepository
    {
        public MemberPaymentDTO GetMemberPayment(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                var meeting = ctx.MemberPayments.Where(d => d.Id == id);
                var entity = meeting.FirstOrDefault();
                Mapper.CreateMap<MemberPayment, MemberPaymentDTO>();
                var dto = Mapper.Map<MemberPayment, MemberPaymentDTO>(entity);
                return dto;
            }
        }

        public List<MemberPaymentDTO> GetAllMemberPayments()
        {
            using (var ctx = new UserGroupEntities())
            {
                var entities = ctx.MemberPayments.ToList();
                Mapper.CreateMap<MemberPayment, MemberPaymentDTO>();
                var dto = Mapper.Map<List<MemberPayment>, List<MemberPaymentDTO>>(entities);
                return dto;
            }
        }

        public int AddNewMemberPayment(MemberPaymentDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<MemberPaymentDTO, MemberPayment>();
                var entity = Mapper.Map<MemberPaymentDTO, MemberPayment>(dto);
                ctx.AddToMemberPayments(entity);
                ctx.SaveChanges();
                return entity.Id;

            }
        }

        public void UpdateMemberPayment(MemberPaymentDTO dto)
        {
            using (var ctx = new UserGroupEntities())
            {
                Mapper.CreateMap<MemberPaymentDTO, MemberPayment>();
                var entity = Mapper.Map<MemberPaymentDTO, MemberPayment>(dto);
                ctx.ApplyCurrentValues("MemberPayments", entity);
                ctx.SaveChanges();
            }
        }

        public void DeleteMemberPaymentDTO(int id)
        {
            using (var ctx = new UserGroupEntities())
            {
                MemberPayment dbDept = ctx.MemberPayments.First(d => d.Id == id);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
